package com.chengyu.core.service.member.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.ConfigEnums;
import com.chengyu.core.domain.enums.MemberTypes;
import com.chengyu.core.domain.enums.RedisEnums;
import com.chengyu.core.domain.form.MemberSearchForm;
import com.chengyu.core.domain.result.CustomerConstant;
import com.chengyu.core.domain.result.MemberDetails;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.*;
import com.chengyu.core.model.*;
import com.chengyu.core.security.util.JwtTokenUtil;
import com.chengyu.core.service.config.ConfigMissionService;
import com.chengyu.core.service.im.ChatService;
import com.chengyu.core.service.member.*;
import com.chengyu.core.service.shop.ShopAccountService;
import com.chengyu.core.service.system.ConfigService;
import com.chengyu.core.service.system.SysInviteCodeService;
import com.chengyu.core.service.walk.WalkMemberService;
import com.chengyu.core.util.ip.IpSearch;
import com.chengyu.core.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author LeGreen
 * @title 用户实现类
 * @date 2021/1/7
 */
@Service
public class MemberServiceImpl implements MemberService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MemberServiceImpl.class);

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private ConfigService configService;
    @Autowired
    private UmsMemberMapper memberMapper;
    @Autowired
    private MemberRelService memberRelService;
    @Autowired
    private MemberGroupService memberGroupService;
    @Autowired
    private ConfigMissionService configMissionService;
    @Autowired
    private MemberAccountService memberAccountService;
    @Autowired
    private UmsMemberLoginLogMapper memberLoginLogMapper;
    @Autowired
    private UmsMemberRelMapper memberRelMapper;
    @Autowired
    private MemberWithdrawService withdrawService;
    @Autowired
    private SysInviteCodeService sysInviteCodeService;
    @Autowired
    private WalkMemberService walkMemberService;
    @Autowired
    private ChatService chatService;
    @Autowired
    private ShopAccountService shopAccountService;
    @Autowired
    private UmsShopMapper umsShopMapper;
    @Autowired
    private UmsShopConfigMapper umsShopConfigMapper;
    @Autowired
    private WalkTrendsDownloadNumMapper walkTrendsDownloadNumMapper;
    @Autowired
    private UmsShopOwnLevelMapper umsShopOwnLevelMapper;
    @Autowired
    private UmsShopLevelUseLogMapper umsShopLevelUseLogMapper;
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void register(UmsMember member, boolean isShop) throws ServiceException {
        //校验code是否已被注册
        UmsMember haveMember = this.getMemberByParams(member.getCode(), MemberTypes.CODE);
        if (haveMember != null) {
            throw new ServiceException("会员已经注册");
        }
        if (StringUtils.isBlank(member.getHeadImg())) {
            member.setHeadImg(configService.getValueByNid(ConfigEnums.ConfigTypes.HEADIMG.getNid()));
        }
        if (StringUtils.isBlank(member.getNickname())) {
            member.setNickname("U" + StringUtils.getSixCode());
        }
        String oriPassword = member.getPassword();
        member.setPassword(passwordEncoder.encode(member.getPassword()));
        if (member.getMethod() == null) {
            member.setMethod(1);
        }
        member.setStatus(CommonConstant.YES_INT);
        member.setAddTime(DateUtil.date());
        //获取会员默认组别
        UmsMemberGroup memberGroup;
        if (member.getType() == CommonConstant.BUYER) {
            memberGroup = memberGroupService.getDefaultBuyerGroup();
        } else {
            memberGroup = memberGroupService.getDefaultSellerGroup();
        }
        if (memberGroup == null) {
            throw new ServiceException("系统尚未设置默认会员组, 注册失败");
        }
        member.setGroupId(memberGroup.getId());
        member.setGroupIcon(memberGroup.getIcon());
        member.setGroupName(memberGroup.getName());
        //获取会员默认佣金组
        ConfigMission configMission = configMissionService.getDefaultConfigMission();
        member.setMissionConfigId(configMission == null ? null : configMission.getId());
        if (StringUtils.isNotBlank(member.getPhone())) {
            member.setPhoneStatus(CommonConstant.SUS_INT);
        }
        member.setInviteCode(sysInviteCodeService.genInviteCode());
        if (member.getTjrId() != null && member.getTjrId() == 0) {
            member.setTjrId(null);
        }
        //获取初始化服务费
        memberMapper.insertSelective(member);

        UmsMember updateMember = new UmsMember();
        updateMember.setId(member.getId());
        String beginIndexStr = configService.getValueByNid("id_begin_index");
        updateMember.setUid((StringUtils.isNotBlank(beginIndexStr) ? Integer.parseInt(beginIndexStr) : 1000000) + member.getId());
        memberMapper.updateByPrimaryKeySelective(updateMember);

        sysInviteCodeService.updateUserId(member.getInviteCode(), CommonConstant.INVITE_CODE_MEMBER, member.getId());

        //注册子账号
        if (isShop) {
            UmsShopAccount account = new UmsShopAccount();
            account.setMemberId(member.getId());
            account.setUsername(member.getCode());
            account.setPassword(oriPassword);
            account.setPhone(member.getPhone());
            account.setMainInd(CommonConstant.YES_INT);
            account.setRealname(member.getCode() + ":主账号");
            shopAccountService.register(account);
        }

        //注册用户分销关系
        if (member.getTjrId() != null) {
            memberRelService.addMemberRel(member.getTjrId(), member.getId());
        }

        //初始化用户账户
        memberAccountService.initAccount(member, memberGroup);

        //初始化创作号
        member.setUid(updateMember.getUid());
        walkMemberService.addWalkMember(member);

        //初始化智能小兵
        UmsMember targetMember = new UmsMember();
        targetMember.setId(CustomerConstant.MEMBER_ID);
        targetMember.setNickname(CustomerConstant.NICKNAME);
        targetMember.setHeadImg(CustomerConstant.AVATAR);
        targetMember.setUid(CustomerConstant.UID);
        chatService.initChatSession(member, targetMember);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateMember(UmsMember updateMember) {
        if (StringUtils.isNotBlank(updateMember.getPassword())) {
            updateMember.setPassword(passwordEncoder.encode(updateMember.getPassword()));
        } else {
            updateMember.setPassword(null);
        }
        if (StringUtils.isNotBlank(updateMember.getPayPassword())) {
            updateMember.setPayPassword(SecureUtil.md5(updateMember.getPayPassword()));
        } else {
            updateMember.setPayPassword(null);
        }
        if (updateMember.getGroupId() != null) {
            UmsMemberGroup group = memberGroupService.getGroup(updateMember.getGroupId());
            updateMember.setType(group.getType());
            updateMember.setGroupName(group.getName());
        }
        updateMember.setUpdTime(DateUtil.date());
        memberMapper.updateByPrimaryKeySelective(updateMember);
    }

    @Override
    public void updateMemberByXcxOpenId(UmsMember member) {
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andXcxOpenIdEqualTo(member.getXcxOpenId());
        UmsMember umsMember = new UmsMember();
        if (StringUtils.isNotBlank(member.getNickname())) {
            umsMember.setNickname(member.getNickname());
        }
        if (StringUtils.isNotBlank(member.getHeadImg())) {
            umsMember.setHeadImg(member.getHeadImg());
        }

        memberMapper.updateByExampleSelective(umsMember, example);
    }

    @Override
    public void updateMemberById(UmsMember member) {
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andIdEqualTo(member.getId());
        UmsMember umsMember = new UmsMember();
        if (StringUtils.isNotBlank(member.getNickname())) {
            umsMember.setNickname(member.getNickname());
        }
        if (StringUtils.isNotBlank(member.getHeadImg())) {
            umsMember.setHeadImg(member.getHeadImg());
        }
        memberMapper.updateByExampleSelective(umsMember, example);
    }

    @Override
    public List<UmsMember> getMemberList(Integer shopId, MemberSearchForm form, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        UmsMemberExample example = new UmsMemberExample();
        if (StringUtils.isNotBlank(form.getSort())) {
            example.setOrderByClause(form.getSort());
        } else {
            example.setOrderByClause("add_time desc");
        }
        UmsMemberExample.Criteria criteria = example.createCriteria();
        if (shopId != null) {
            criteria.andShopIdEqualTo(shopId);
        }
        if (StringUtils.isNotBlank(form.getCode())) {
            criteria.andCodeLike("%" + form.getCode() + "%");
        }
        if (StringUtils.isNotBlank(form.getRealname())) {
            criteria.andRealnameLike("%" + form.getRealname() + "%");
        }
        if (StringUtils.isNotBlank(form.getNickname())) {
            criteria.andNicknameLike("%" + form.getNickname() + "%");
        }
        if (StringUtils.isNotBlank(form.getPhone())) {
            criteria.andPhoneLike("%" + form.getPhone() + "%");
        }
        if (form.getMethod() != null) {
            criteria.andMethodEqualTo(form.getMethod());
        }
        if (form.getStatus() != null) {
            criteria.andStatusEqualTo(form.getStatus());
        }
        if (form.getPhoneStatus() != null) {
            criteria.andPhoneStatusEqualTo(form.getPhoneStatus());
        }
        if (form.getRealnameStatus() != null) {
            criteria.andRealnameStatusEqualTo(form.getRealnameStatus());
        }
        if (form.getBankStatus() != null) {
            criteria.andBankStatusEqualTo(form.getBankStatus());
        }
        if (form.getTjrId() != null) {
            criteria.andTjrIdEqualTo(form.getTjrId());
        }
        if (form.getDateFrom() != null && form.getDateTo() != null) {
            criteria.andAddTimeBetween(form.getDateFrom(), form.getDateTo());
        }
        if (form.getGroupId() != null) {
            criteria.andGroupIdEqualTo(form.getGroupId());
        }
        if (form.getType() != null) {
            criteria.andTypeEqualTo(form.getType());
        }
        if (StringUtils.isNotBlank(form.getRegisterIp())) {
            criteria.andRegisterIpEqualTo(form.getRegisterIp());
        }
        if (form.getMemberId() != null) {
            criteria.andIdEqualTo(form.getMemberId());
        }
        if (StringUtils.isNotBlank(form.getQq())) {
            criteria.andQqEqualTo(form.getQq());
        }
        return memberMapper.selectByExample(example);
    }

    @Override
    public UmsMember getMemberById(Integer id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public UmsMember getMemberByParams(String params, MemberTypes type) {
        UmsMemberExample example = new UmsMemberExample();
        UmsMemberExample.Criteria criteria = example.createCriteria();
        switch (type) {
            case CODE:
                criteria.andCodeEqualTo(params);
                break;

            case KF_OPEN_ID:
                criteria.andKfOpenIdEqualTo(params);
                break;

            case XCX_OPEN_ID:
                criteria.andXcxOpenIdEqualTo(params);
                break;

            case GZH_OPEN_ID:
                criteria.andGzhOpenIdEqualTo(params);
                break;

            case UNIONID:
                criteria.andUnionidEqualTo(params);
                break;

            case PHONE:
                criteria.andPhoneEqualTo(params);
                break;

            case INVITE_CODE:
                criteria.andInviteCodeEqualTo(params);
                break;
            case UID:
                criteria.andUidEqualTo(Integer.parseInt(params));
                break;
            default:
                break;
        }
        List<UmsMember> list = memberMapper.selectByExample(example);
        return (list != null && !list.isEmpty()) ? list.get(0) : null;
    }

    @Override
    public UmsMember getCurrentMember() {
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            return memberDetails.getUmsMember();
        } catch (Exception e) {
            LOGGER.info(e.getMessage());
        }

        return null;
    }

    @Override
    public UmsMember getCurrentMemberOrNull() {
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            String nullKey = "anonymousUser";
            if (nullKey.equals(auth.getPrincipal())) {
                return null;
            }

            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            return memberDetails.getUmsMember();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public UserDetails loadUserByUsername(String username) {
        UmsMember member = getMemberByParams(username, MemberTypes.CODE);
        if (member == null) {
            member = getMemberByParams(username, MemberTypes.PHONE);
        }
        if (member != null) {
            return new MemberDetails(member);
        }
        throw new UsernameNotFoundException("用户名或密码错误");
    }

    @Override
    public String login(String username, String password, String ip) {
        String token = null;
        //密码需要客户端加密后传递
        UserDetails userDetails = null;
        UmsMember member = getMemberByParams(username, MemberTypes.CODE);
        if (member == null) {
            member = getMemberByParams(username, MemberTypes.PHONE);
        }
        if (member != null) {
            userDetails = new MemberDetails(member);
        }
        if (userDetails == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        try {
            if (!passwordEncoder.matches(password, userDetails.getPassword())) {
                throw new BadCredentialsException("密码不正确");
            }
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            token = jwtTokenUtil.generateToken(userDetails);
            this.insertLoginLog(member, ip);
        } catch (AuthenticationException e) {
            LOGGER.warn("登录异常:{}", e.getMessage());
        }
        return token;
    }

    @Override
    public String loginByNoPassword(String username) {
        String token = null;
        //密码需要客户端加密后传递
        try {
            UserDetails userDetails = loadUserByUsername(username);
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            token = jwtTokenUtil.generateToken(userDetails);
        } catch (AuthenticationException e) {
            LOGGER.warn("登录异常:{}", e.getMessage());
        }
        return token;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void bindPhone(Integer deleteId, UmsMember member) {
        if (deleteId != null) {
            memberMapper.deleteByPrimaryKey(deleteId);
        }
        memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public UmsMemberLoginLog getLastLoginLog(Integer memberId) {
        UmsMemberLoginLogExample example = new UmsMemberLoginLogExample();
        example.setOrderByClause("add_time desc limit 2");
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<UmsMemberLoginLog> list = memberLoginLogMapper.selectByExample(example);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        } else if (list.size() == 1) {
            return list.get(0);
        } else {
            return list.get(1);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateInvite(Integer memberId, String inviteUid) throws ServiceException {
        UmsMember member = this.getMemberByParams(inviteUid, MemberTypes.UID);
        if (member == null) {
            throw new ServiceException("member.spread.id");
        }
        if (member.getId().equals(memberId)) {
            throw new ServiceException("member.spread.myself");
        }

        UmsMember updateMember = new UmsMember();
        updateMember.setId(memberId);
        updateMember.setTjrId(member.getId());
        updateMember.setTjrUid(member.getUid());
        memberMapper.updateByPrimaryKeySelective(updateMember);

        UmsMemberRelExample example = new UmsMemberRelExample();
        example.createCriteria().andDownMemberIdEqualTo(memberId);
        memberRelMapper.deleteByExample(example);

        memberRelService.addMemberRel(updateMember.getTjrId(), updateMember.getId());

        //调整人的下级也要跟着调整
        List<UmsMember> memberList = memberRelService.getAllDownMemberByMemberId(memberId);
        if (CollectionUtil.isNotEmpty(memberList)) {
            for (UmsMember adMember : memberList) {
                example = new UmsMemberRelExample();
                example.createCriteria().andDownMemberIdEqualTo(adMember.getId());
                memberRelMapper.deleteByExample(example);

                memberRelService.addMemberRel(adMember.getTjrId(), adMember.getId());
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void limitWithdraw(Integer memberId, Integer status, UmsShop shop) throws ServiceException {
        UmsMember updateMember = new UmsMember();
        updateMember.setId(memberId);
        updateMember.setWithdrawLimit(status);
        memberMapper.updateByPrimaryKeySelective(updateMember);

        //驳回当前申请的提现
        if (status == CommonConstant.YES_INT) {
            withdrawService.rejectAllWithdraw(memberId, shop);
        }
    }

    /**
     * 添加登录记录
     *
     * @param member
     * @param ip
     * @author LeGreen
     * @date 2021/1/6
     */
    private void insertLoginLog(UmsMember member, String ip) {
        UmsMemberLoginLog loginLog = new UmsMemberLoginLog();
        loginLog.setMemberId(member.getId());
        loginLog.setMemberName(member.getCode());
        loginLog.setAddTime(new Date());
        loginLog.setAddBy(member.getCode());
        loginLog.setIp(ip);
        loginLog.setAddress(IpSearch.getInstance().getIpAddress(ip));
        memberLoginLogMapper.insert(loginLog);
    }

    @Override
    public List<UmsMember> getAllShopServices() {
        UmsMemberExample example = new UmsMemberExample();
        example.setOrderByClause("id asc");
        example.createCriteria().andStatusEqualTo(CommonConstant.YES_INT);
        List<UmsMember> list = memberMapper.selectByExample(example);
        return list;
    }

    @Override
    public UmsShop getMemberShopById(Integer shopId) throws ServiceException {
        if (shopId == null) {
            throw new ServiceException("您暂未认证关联商家");
        }
        UmsShop umsShop = umsShopMapper.selectByPrimaryKey(shopId);
        return umsShop;
    }

    @Override
    public Map shopAuth(String invitationCode, UmsMember umsMember, WalkMember walkMember) throws ServiceException {
        Integer isAuth = umsMember.getShopStatus();
        //先获取到邀请码对应的会员
        UmsMemberExample exampleInvite = new UmsMemberExample();
        UmsMemberExample.Criteria criteria = exampleInvite.createCriteria();
        criteria.andInviteCodeEqualTo(invitationCode);
        List<UmsMember> umsMemberList = memberMapper.selectByExample(exampleInvite);
        memberMapper.selectByExample(exampleInvite);
        if (umsMemberList.size() == 0) {
            throw new ServiceException("此邀请码不存在");
        }
        if (umsMemberList.get(0).getType() == 1) {
            throw new ServiceException("此邀请码所属会员不是商家");
        }
        //根据会员的id获取商家
        UmsShopExample exampleShop = new UmsShopExample();
        UmsShopExample.Criteria criteriaShop = exampleShop.createCriteria();
        criteriaShop.andMemberIdEqualTo(umsMemberList.get(0).getId());
        List<UmsShop> umsShopList = umsShopMapper.selectByExample(exampleShop);
        if (umsShopList.size() == 0) {
            throw new ServiceException("邀请码对应会员没有拥有商家");
        }



        UmsShopOwnLevelExample exampleOwnLevel = new UmsShopOwnLevelExample();
        UmsShopOwnLevelExample.Criteria criteriaOwn = exampleOwnLevel.createCriteria();
        criteriaOwn.andShopIdEqualTo(umsShopList.get(0).getId());
        List<Integer> types = new ArrayList<>();
        types.add(1);
        criteriaOwn.andTypeIn(types);
        criteriaOwn.andDeadlineGreaterThan(new Date());
        List<UmsShopOwnLevel> umsShopOwnLevels = umsShopOwnLevelMapper.selectByExample(exampleOwnLevel);
        if (isAuth == 0) {
            //判断商家会员绑定达人剩余数量是否有剩余
            Integer totalNum = 0;
            for (UmsShopOwnLevel umsShopOwnLevel : umsShopOwnLevels) {
                totalNum = totalNum + umsShopOwnLevel.getResidueBindExpertNum();
            }
            if (totalNum == 0) {
                throw new ServiceException("商家剩余绑定达人数量不足，请联系商家");
            }
        }


        UmsMember updateMember = getCurrentMember();
        updateMember.setId(umsMember.getId());
        updateMember.setShopStatus(1);
        updateMember.setShopId(umsShopList.get(0).getId());
        updateMember.setAuthShop(umsShopList.get(0).getName());
        updateMember.setAuthDate(new Date());
        updateMember.setShopMemberId(umsMemberList.get(0).getId());
        updateMember.setPassword(null);
        updateMember.setPayPassword(null);
        updateMember.setGroupId(null);
        updateMember.setAuthFromInvitCode(invitationCode);
        memberMapper.updateByPrimaryKeySelective(updateMember);


        if (isAuth == 0) {
            //添加成功后，扣减此商家的剩余绑定次数
            Integer currentTotalDataNum = 1;
            for (UmsShopOwnLevel umsShopOwnLevel : umsShopOwnLevels) {
                if (umsShopOwnLevel.getResidueBindExpertNum() <= currentTotalDataNum) {
                    //如何这条拥有数量小于上传数量直接扣减掉
                    UmsShopOwnLevel umsShopOwnLevelUpdate = new UmsShopOwnLevel();
                    umsShopOwnLevelUpdate.setResidueBindExpertNum(0);
                    umsShopOwnLevelUpdate.setId(umsShopOwnLevel.getId());
                    umsShopOwnLevelMapper.updateByPrimaryKeySelective(umsShopOwnLevelUpdate);
                    currentTotalDataNum = currentTotalDataNum - umsShopOwnLevel.getResidueBindExpertNum();
                } else if (umsShopOwnLevel.getResidueBindExpertNum() > currentTotalDataNum) {
                    UmsShopOwnLevel umsShopOwnLevelUpdate = new UmsShopOwnLevel();
                    umsShopOwnLevelUpdate.setResidueBindExpertNum(umsShopOwnLevel.getResidueBindExpertNum() - currentTotalDataNum);
                    umsShopOwnLevelUpdate.setId(umsShopOwnLevel.getId());
                    umsShopOwnLevelMapper.updateByPrimaryKeySelective(umsShopOwnLevelUpdate);
                }
            }
            //添加会员等级使用记录-绑定达人
            UmsShopLevelUseLog umsShopLevelUseLog = new UmsShopLevelUseLog();
            umsShopLevelUseLog.setShopId(umsShopList.get(0).getId());
            umsShopLevelUseLog.setType(4);
            umsShopLevelUseLog.setNum(1.0);
            umsShopLevelUseLog.setBindId(umsMember.getId());
            umsShopLevelUseLog.setAddTime(new Date());
            umsShopLevelUseLogMapper.insert(umsShopLevelUseLog);
        }

        Map map = new HashMap();
        map.put("verityMember", updateMember);
        if (isAuth == 0) {//如果没有认证 才去添加
            //认证成功后，添加初始使用次数
            //先从配置中查到这个店铺的初始次数配置
            UmsShopConfigExample exampleConfig = new UmsShopConfigExample();
            UmsShopConfigExample.Criteria criteriaConfig = exampleConfig.createCriteria();
            criteriaConfig.andShopIdEqualTo(umsShopList.get(0).getId());
            List<UmsShopConfig> umsShopConfigList = umsShopConfigMapper.selectByExample(exampleConfig);
            Integer initialAuthDownloadNumVideo = umsShopConfigList.get(0).getInitialAuthDownloadNumVideo();
            Integer initialAuthDownloadNumImg = umsShopConfigList.get(0).getInitialAuthDownloadNumImg();
            //插入到下载次数列表
            WalkTrendsDownloadNum walkTrendsDownloadNum = new WalkTrendsDownloadNum();
            walkTrendsDownloadNum.setDownloadWalkMemberId(walkMember.getId());
            walkTrendsDownloadNum.setDownloadWalkMemberName(walkMember.getNickname());
            walkTrendsDownloadNum.setDownloadWalkMemberHeadImg(walkMember.getHeadImg());
            walkTrendsDownloadNum.setShopId(umsShopList.get(0).getId());
            walkTrendsDownloadNum.setStatus(1);
            walkTrendsDownloadNum.setSource(1);
            walkTrendsDownloadNum.setAddTime(DateUtil.date());


            if (initialAuthDownloadNumVideo != null && initialAuthDownloadNumVideo != 0) {
                map.put("initialAuthDownloadNumVideo", initialAuthDownloadNumVideo);
                walkTrendsDownloadNum.setType(1);
                walkTrendsDownloadNum.setNum(initialAuthDownloadNumVideo);
                walkTrendsDownloadNum.setUseNum(initialAuthDownloadNumVideo);
                walkTrendsDownloadNumMapper.insert(walkTrendsDownloadNum);
            }
            if (initialAuthDownloadNumImg != null && initialAuthDownloadNumImg != 0) {
                map.put("initialAuthDownloadNumImg", initialAuthDownloadNumImg);
                walkTrendsDownloadNum.setType(3);
                walkTrendsDownloadNum.setNum(initialAuthDownloadNumImg);
                walkTrendsDownloadNum.setUseNum(initialAuthDownloadNumImg);
                walkTrendsDownloadNumMapper.insert(walkTrendsDownloadNum);
            }
        }
        return map;
    }
}